package test;

// http://www.informit.com/articles/article.asp?p=24130&seqNum=5&rl=1
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;

class TableDemo2 extends JFrame implements ActionListener,
                      TableModelListener
{
  JTable jt;

  TableDemo2 (String title)
  {
   // Pass the title to the JFrame superclass so that it appears in
   // the title bar.

   super (title);

   // Tell the program to exit when the user either selects Close
   // from the System menu or presses an appropriate X button on the
   // title bar.

   setDefaultCloseOperation (EXIT_ON_CLOSE);

   // Create a default table model consisting of 4 rows by 2 
   // columns.

   DefaultTableModel dtm = new DefaultTableModel (4, 2);

   // Populate all cells in the default table model.

   String [] names =
   {
     "John Doe",
     "Jane Smith",
     "Jack Jones",
     "Paul Finch"
   };

   String [] addresses =
   {
     "200 Fox Street",
     "Apt. 555",
     "Box 9000",
     "1888 Apple Avenue"
   };

   int nrows = dtm.getRowCount ();

   for (int i = 0; i < nrows; i++)
   {
      dtm.setValueAt (names [i], i, 0);
      dtm.setValueAt (addresses [i], i, 1);
   }

   // Register the current TableDemo2 object as a listener for table
   // model events.

   dtm.addTableModelListener (this);

   // Create a table using the previously created default table
   // model.

   jt = new JTable (dtm);

   // Add the table to the center portion of the frame window's
   // content pane.

   getContentPane ().add (jt);

   // Create a panel for positioning buttons.

   JPanel jp = new JPanel ();

   // Create a "Delete Last Row" button, register the current
   // TableDemo2 object as a listener to that button's action
   // events, and add that button to the panel.

   JButton jb = new JButton ("Delete Last Row");
   jb.addActionListener (this);
   jp.add (jb);

   // Create a "Append Row" button, register the current TableDemo2
   // object as a listener to that button's action events, and add
   // that button to the panel.

   jb = new JButton ("Append Row");
   jb.addActionListener (this);
   jp.add (jb);

   // Add the panel to the south portion of the frame window's
   // content pane.

   getContentPane ().add (jp, BorderLayout.SOUTH);

   // Establish the overall size of the frame window to 400
   // horizontal pixels by 175 vertical pixels.

   setSize (400, 175);

   // Display the frame window and all contained
   // components/containers.

   setVisible (true);
  }

  public void actionPerformed (ActionEvent e)
  {
   // Identify the button that initiated the event.

   JButton jb = (JButton) e.getSource ();

   // Obtain the button's label.

   String label = jb.getText ();

   // Either delete or append a row, as appropriate.

   if (label.equals ("Delete Last Row"))
   {
     DefaultTableModel dtm = (DefaultTableModel) jt.getModel ();
     int nRows = dtm.getRowCount ();
     if (nRows != 0)
       dtm.removeRow (nRows - 1);
   }
   else
   {
     DefaultTableModel dtm = (DefaultTableModel) jt.getModel ();
     String [] data = { "Name", "Address" };
     dtm.insertRow (dtm.getRowCount (), data);
   }
  }

  public void tableChanged (TableModelEvent e)
  {
   // Identify what has changed.

   switch (e.getType ())
   {
     case TableModelEvent.DELETE: System.out.println ("Delete");
                   break;
       
     case TableModelEvent.INSERT: System.out.println ("Insert");
                   break;

     case TableModelEvent.UPDATE: System.out.println ("Update");
   }
  }

  public static void main (String [] args)
  {
   // Create a TableDemo2 object, which creates the GUI.

   new TableDemo2 ("Table Demo #2");
  }
}